
package com.scalar.chiptrack.operations.process.actions;


import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.operations.process.VendorLocation;
import com.scalar.chiptrack.operations.process.dao.FabDAO;
import com.scalar.chiptrack.operations.process.forms.FabForm;
import com.scalar.chiptrack.operations.process.forms.FabProcessRecipeForm;
import com.scalar.chiptrack.operations.process.forms.FabRecipeForm;




public final class FabProcessRecipeAction extends ChipTrackAction {

    public FabProcessRecipeAction() {
    }

    public ActionForward execute (ActionMapping mapping, 
                                  ActionForm form, 
                                  HttpServletRequest request, 
                                  HttpServletResponse response) throws Exception {

			 HttpSession ses = request.getSession(true);
			 String recipeId = (String)ses.getAttribute("recipeId");
			 FabForm fabForm = (FabForm) form;
			 String processStepId = fabForm.getProcessStepId();
			 ArrayList recipeList = new ArrayList();
			 FabRecipeForm fabRecipeForm = null;
			 
			 if(recipeId==null)	
			 recipeId = "";
			
			if(processStepId!=null && !processStepId.equals("") ){
				// for exixting
			
		    //System.out.println("In existing ");		
			recipeList=FabDAO.loadRecipeData(processStepId);
							
			}

			else{
			
			// for new 

			fabRecipeForm = (FabRecipeForm)ses.getAttribute("recipe");

			if(fabRecipeForm!=null)
			{
			  String parameter[] =fabRecipeForm.getParameter();
			  String maxValue[]  =fabRecipeForm.getMaxValue();
			  String minValue[]  =fabRecipeForm.getMinValue();
			  
			    for(int i=0; i<parameter.length;i++)
			{
				if(!parameter[i].equals(""))
				{
				FabProcessRecipeForm fabProcessRecipeForm = new FabProcessRecipeForm();
				fabProcessRecipeForm.setParameter(parameter[i]);
				fabProcessRecipeForm.setMaxValue(maxValue[i]);
				fabProcessRecipeForm.setMinValue(minValue[i]);
				recipeList.add(fabProcessRecipeForm);
				}

				//System.out.println("parameter in FabProcessRecipeAction "+parameter[i]);
			}
        
			
			}
			
			}	
			
			ArrayList vendorLocationList = new ArrayList();
			VendorLocation vendorLocation = null;
			FormFile procDocFile =fabForm.getProcDocFile();

			//System.out.println("procDocFile Name = "+ procDocFile.getFileName());

			fabForm.setProcDocFile(procDocFile);

			ses.setAttribute("fabForm",fabForm);

			//System.out.println("fabname in recipe "+fabForm.getFabName());
			
			int [] locationID		= fabForm.getLocationID();
			int [] priority			= fabForm.getPriority();
			String [] locationName	= fabForm.getLocationName();
			int [] split			= fabForm.getSplit();
			int [] capacityWeek		= fabForm.getCapacityWeek();
			float [] normalCost		= fabForm.getNormalCost();
			float [] expressCost	= fabForm.getExpressCost();
			int [] normalCycleTime	= fabForm.getNormalCycleTime();
			int [] expressCycleTime	= fabForm.getExpressCycleTime();
			float [] expectedYieid  = fabForm.getExpectedYield();
			String select = null;

			
			if(locationName!=null)
			{
			for(int i=0;i<locationName.length;i++){
				//System.out.println("expressCycleTime in For "+expressCycleTime[i]);
			
				select = request.getParameter("checkLoc"+i);
				vendorLocation=new VendorLocation();
				vendorLocation.setLocationID(locationID[i]+"");
				vendorLocation.setPriority(priority[i]+"");
				vendorLocation.setLocationName(locationName[i]);
				vendorLocation.setSplit(split[i]+"");
				vendorLocation.setCapacityWeek(capacityWeek[i]+"");
				vendorLocation.setNormalCost(normalCost[i]);
				vendorLocation.setExpressCost(expressCost[i]);
				vendorLocation.setNormalCycleTime(normalCycleTime[i]+"");
				vendorLocation.setExpressCycleTime(expressCycleTime[i]+"");
				vendorLocation.setExpectedYield(expectedYieid[i]);
				if (select != null) {
				vendorLocation.setSelect (select);
				}
				vendorLocationList.add(vendorLocation);	
				
			}

			}

			else{
				//System.out.println("Hey In else");
				vendorLocationList=(ArrayList)ses.getAttribute("vendorLocations");
				//System.out.println("Size of the VendorList "+vendorLocationList.size());
				}		
			ses.setAttribute("vendorLocations",vendorLocationList);
			request.setAttribute("recipeList",recipeList);
		    return (mapping.findForward("success")); 

	}
	
}